using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using MoreGen.Util;

namespace MoreGen.MapTypes
{
    public enum ColumnTypes
    {
        Normal,
        Collection
    }

    public enum CollectionTypes
    {
        Bag,
        Map,
        List
    }

    public enum ForeignKeyTypes
    {
        KeyOnly,
        ObjectOnly,
        KeyObjectRO,
        ObjectKeyRO
    }

    public class ColumnMap
    {
        internal ColumnMap()
        {
            _Table = TableMap.CurrentSerial;
        }
        public ColumnMap(TableMap t)
        {
            _Table = t;
        }

        private TableMap _Table;
        [XmlIgnore()]
        public TableMap Table
        {
            get { return _Table; }
        }

        [XmlIgnore()]
        public string FieldName
        {
            get
            {
                return "_" + PropertyName;
            }
        }
        [XmlIgnore()]
        public string ObjectFieldName
        {
            get
            {
                return "_" + ObjectPropertyName;
            }
        }

        private string _ColumnName;
        [Category("Database")]
        public string ColumnName
        {
            get { return _ColumnName; }
            set
            {
                _ColumnName = value;
                if (_ColumnName != Table.ClassName)
                    _PropertyName = FixCaseName();
                else
                {
                    if (DataType == "string")
                        _PropertyName = FixCaseName() + "Text";
                    else
                        _PropertyName = FixCaseName() + "Value";
                }
            }
        }


        private string _PropertyName;
        [Category("Class")]
        public string PropertyName
        {
            get { return _PropertyName; }
            set { _PropertyName = value; }
        }
        [XmlIgnore()]
        public string ObjectPropertyName
        {
            get 
            {
                if (PropertyName.ToUpper().EndsWith("ID"))
                {
                    return PropertyName.Substring(0, PropertyName.Length - 2);
                }

                return PropertyName;
            }
        }
	
        private string _DataType;
        public string DataType
        {
            get { return _DataType; }
            set { _DataType = value; }
        }

        private string _DBType;
        public string DBType
        {
            get { return _DBType; }
            set { _DBType = value; }
        }

        private bool _IsPK;
        public bool IsPK
        {
            get { return _IsPK; }
            set { _IsPK = value; }
        }

        private bool _Nullable;
        public bool Nullable
        {
            get { return _Nullable; }
            set { _Nullable = value; }
        }

        private bool _ReadOnly;
        public bool ReadOnly
        {
            get 
            {
                if (Table.ReadOnly)
                    return true;

                return _ReadOnly; 
            }
            set { _ReadOnly = value; }
        }

        private bool _Identity;
        public bool Identity
        {
            get { return _Identity; }
            set { _Identity = value; }
        }

        private bool _Calculated;
        public bool Calculated
        {
            get { return _Calculated; }
            set { _Calculated = value; }
        }

        private ColumnTypes _ColumnType;
        public ColumnTypes ColumnType
        {
            get { return _ColumnType; }
            set { _ColumnType = value; }
        }

        private CollectionTypes _CollectionType;
        public CollectionTypes CollectionType
        {
            get { return _CollectionType; }
            set { _CollectionType = value; }
        }
	
        private string _CollectionColumn;
        public string CollectionColumn
        {
            get { return _CollectionColumn; }
            set { _CollectionColumn = value; }
        }

        private string _CollectionTable;
        public string CollectionTable
        {
            get { return _CollectionTable; }
            set { _CollectionTable = value; }
        }

        private string _IndexColumn;
        public string IndexColumn
        {
            get { return _IndexColumn; }
            set { _IndexColumn = value; }
        }

        private bool _IsFK;
        public bool IsFK
        {
            get { return _IsFK; }
            set { _IsFK = value; }
        }

        private ForeignKeyTypes _FKType = ForeignKeyTypes.ObjectOnly;
        public ForeignKeyTypes ForeignKeyType
        {
            get { return _FKType; }
            set { _FKType = value; }
        }

        private string _Sequence;
        public string Sequence
        {
            get { return _Sequence; }
            set { _Sequence = value; }
        }
	

        [XmlIgnore()]
        public string ForeignType
        {
            get
            {
                return ForeignTable.ClassName;
            }
        }

        [XmlIgnore()]
        public TableMap ForeignTable
        {
            get
            {
                return Table.Project.Tables[CollectionTable];
            }
        }

        public override string ToString()
        {
            return PropertyName;
        }

        private string FixCaseName()
        {
            if (!Table.Project.Database.FixCase)
                return ColumnName;

            char[] t = ColumnName.ToLower().ToCharArray();

            for (int i = 0; i < t.Length; ++i)
            {
                if (t[i] == '_')
                {
                    if ((i + 1) < t.Length)
                        t[i + 1] = Char.ToUpper(t[i + 1]);
                    continue;
                }
                if (i == 0)
                    t[i] = char.ToUpper(t[0]);
            }

            return new String(t);
        }
    }
}
